#!/bin/bash
# Job yaml can send below ENVIRONMENT VARIABLES to me
# - os
# - os_version
# - test_os_version
# - net_acc_enable
# END ENVIRONMENT VARIABLES

. $LKP_SRC/lib/log.sh
. $LKP_SRC/lib/upload.sh

check_yaml_vars()
{
    log_info "starting check yaml vars ..."

    local yaml_vars=(
        "os"
        "os_version"
        "test_os_version"
    )

    for yaml_t in "${yaml_vars[@]}"
    do
        [ -n "$(eval echo "\$${yaml_t}")" ] || die "cannot find value of var: ${yaml_t}."
    done
}

handle_docker_image() {
    ip route add default via 172.168.131.2
    yum install -y kernel-tools docker make gcc gcc-c++

    mkdir -p /etc/systemd/system/docker.service.d
    cat>/etc/systemd/system/docker.service.d/10-ramdisk.conf<<EOF
[Service]
Environment=DOCKER_RAMDISK=true
EOF
    systemctl daemon-reload
    systemctl restart docker

    export IMG_PATH="/root/openeuler_docker"
    export docker_img=${IMG_PATH}/openEuler-docker.$(uname -m).tar.xz
    export image_name="openeuler-$(echo "${test_os_version}" | awk '{print tolower($0)}')"
    mkdir -p ${IMG_PATH}
        
    source="https://repo.openeuler.openatom.cn/openEuler-${test_os_version}/docker_img/$(uname -m)/openEuler-docker.$(uname -m).tar.xz"
    wget $source -P ${IMG_PATH}
}

set_env () {
    docker stop $(docker ps -a -q)
    docker rm $(docker ps -a -q)
}

redis_depoly() {
    set_env
    arch=$(uname -m)
    redis_version="5.0.14"
    docker_mem="4g"
    docker_cpus=2
    iter_num=10
    if [[ "$arch" == "aarch64" ]];then
        yum install -y libatomic
    fi

    docker load -i $docker_img

    image_id=$(docker images | grep ${image_name} | awk -F " " '{print $3}')
    redis_server=$(docker ps --all|grep "redis-server"|wc -l)
    redis_client=$(docker ps --all |grep "redis-client"|wc -l)

    if [ $net_acc_enable -eq 1 ];then
        net-acc enable
    fi

    if [ $redis_server -eq 1 ];then
        redis_server=0
        docker rm -f redis-server
    fi

    if [ $redis_client -eq 1 ];then
        redis_client=0
        docker rm -f redis-client
    fi

    if [ $redis_server -eq 0 ];then
        docker run -itd -m $docker_mem --cpus $docker_cpus --name redis-server $image_id /bin/bash
    fi

    if [ $redis_client -eq 0 ];then
        docker run -itd -m $docker_mem --cpus $docker_cpus --name redis-client $image_id /bin/bash
    fi

    echo 0 > /proc/sys/net/bridge/bridge-nf-call-iptables

    if [ ! -f "redis-${redis_version}.tar.gz" ];then
        wget http://download.redis.io/releases/redis-${redis_version}.tar.gz
    fi

    if [ ! -d "redis-${redis_version}" ];then
        tar xf redis-${redis_version}.tar.gz
        cd redis-${redis_version} && make -j`nproc` > /dev/null
        cd -
    fi

    cd redis-${redis_version}

    # get the hostip for redis_server docker
    ip=$(ip a | grep docker0 | grep inet | awk -F ' ' '{print $2}' | awk -F '/' '{print $1}')
    server_host_ip=$(echo $ip | sed  "s/.1$/.2/")
    sed -i "s/^bind 127.0.0.1/bind ${server_host_ip}/g" redis.conf
    sed -i "s/^daemonize no/daemonize yes/g" redis.conf

    redis_server_id=$(docker ps |grep "redis-server"| awk -F " " '{print $1}')
    redis_client_id=$(docker ps |grep "redis-client"| awk -F " " '{print $1}')

    docker cp src/redis-benchmark $redis_client_id:/home
    docker cp src/redis-server $redis_server_id:/home
    docker cp redis.conf $redis_server_id:/home
    cd -

    if [[ "$arch" == "aarch64" ]];then
        docker cp /usr/lib64/libatomic.so.1.* $redis_client_id:/usr/lib64/
        docker cp /usr/lib64/libatomic.so.1 $redis_client_id:/usr/lib64/
        docker cp /usr/lib64/libatomic.so.1.* $redis_server_id:/usr/lib64/
        docker cp /usr/lib64/libatomic.so.1 $redis_server_id:/usr/lib64/
    fi

    docker cp /usr/bin/taskset $redis_client_id:/usr/bin
    docker cp /usr/bin/taskset $redis_server_id:/usr/bin
}

redis_test() {
    cat>redis_test.sh<<EOF
#! /bin/bash
test_ip=${server_host_ip}
cd /home

for i in \`seq 1 ${iter_num}\`;do
    taskset -c 7 ./redis-benchmark -n 1000000 -c 50 -h \${test_ip} -q -t set,get -d 1024  --csv >> p1_1024.log
done

cat p1_1024.log | grep SET | awk -F \" '{print \$4}' > p1_1024_set.xls
cat p1_1024.log | grep GET | awk -F \" '{print \$4}' > p1_1024_get.xls

for i in \`seq 1 ${iter_num}\`;do
    taskset -c 7 ./redis-benchmark -n 1000000 -c 50 -h \${test_ip} -q -t set,get -d 256  --csv >> p1_256.log
done

cat p1_256.log | grep SET | awk -F \" '{print \$4}' > p1_256_set.xls
cat p1_256.log | grep GET | awk -F \" '{print \$4}' > p1_256_get.xls

for i in \`seq 1 ${iter_num}\`;do
    taskset -c 7 ./redis-benchmark -n 1000000 -c 50 -h \${test_ip} -q -P 20 -t set,get --csv >> p20.log
done

cat p20.log | grep SET | awk -F \" '{print \$4}' > p20_set.xls
cat p20.log | grep GET | awk -F \" '{print \$4}' > p20_get.xls
EOF
}

redis_test_execute() {
    # run redis_server
    redis_test
    docker exec redis-server bash -c "taskset -c 6 /home/redis-server /home/redis.conf"
    docker cp redis_test.sh redis-client:/home

    # run redis_test
    docker exec redis-client bash -c "bash /home/redis_test.sh"

    if [ $net_acc_enable -eq 1 ];then
        net-acc disable
    fi
}

collect_docker_data() {
    docker exec redis-client bash -c "mkdir -p /home/redis_test_result"
    docker exec redis-client bash -c "mv /home/*.xls /home/redis_test_result"
    docker cp redis-client:/home/redis_test_result .
    docker rm -f $redis_server_id
    docker rm -f $redis_client_id

    cd /redis_test_result/
    for file in ./*; do
        if [ -f "$file" ]; then
            upload_one_curl "${file}" "${result_root}"
        fi
    done
}

post_clean() {
    rm -rf redis-${redis_version}
    docker rm -f $redis_server_id
    docker rm -f $redis_client_id
}

check_yaml_vars
handle_docker_image
redis_depoly
redis_test_execute
collect_docker_data
